package com.flyco.modules.oms.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.flyco.modules.oms.entity.OmsElemeRetailOrder;
import com.flyco.modules.oms.entity.OmsElemeGoodsItem;
import com.flyco.modules.oms.mapper.OmsElemeGoodsItemMapper;
import com.flyco.modules.oms.mapper.OmsElemeRetailOrderMapper;
import com.flyco.modules.oms.service.IOmsElemeRetailOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 饿了么订单表
 * @Author: flyco
 * @Date:   2021-08-10
 * @Version: V1.0
 */
@DS("store")
@Service
public class OmsElemeRetailOrderServiceImpl extends ServiceImpl<OmsElemeRetailOrderMapper, OmsElemeRetailOrder> implements IOmsElemeRetailOrderService {

	@Autowired
	private OmsElemeRetailOrderMapper omsElemeRetailOrderMapper;
	@Autowired
	private OmsElemeGoodsItemMapper omsElemeGoodsItemMapper;
	
	@Override
	@Transactional
	public void saveMain(OmsElemeRetailOrder omsElemeRetailOrder, List<OmsElemeGoodsItem> omsElemeGoodsItemList) {
		omsElemeRetailOrderMapper.insert(omsElemeRetailOrder);
		for(OmsElemeGoodsItem entity:omsElemeGoodsItemList) {
			//外键设置
			entity.setOrderId(omsElemeRetailOrder.getId());
			omsElemeGoodsItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(OmsElemeRetailOrder omsElemeRetailOrder,List<OmsElemeGoodsItem> omsElemeGoodsItemList) {
		omsElemeRetailOrderMapper.updateById(omsElemeRetailOrder);
		
		//1.先删除子表数据
		omsElemeGoodsItemMapper.deleteByMainId(omsElemeRetailOrder.getId());
		
		//2.子表数据重新插入
		for(OmsElemeGoodsItem entity:omsElemeGoodsItemList) {
			//外键设置
			entity.setOrderId(omsElemeRetailOrder.getId());
			omsElemeGoodsItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		omsElemeGoodsItemMapper.deleteByMainId(id);
		omsElemeRetailOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			omsElemeGoodsItemMapper.deleteByMainId(id.toString());
			omsElemeRetailOrderMapper.deleteById(id);
		}
	}
	
}
